package com.ych.redis.init;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @BelongsProject: ape-frame
 * @BelongsPackage: com.ych.redis.init
 * @Author: ych
 * @CreateTime: 2024-11-26  12:09
 * @Description: TODO
 * @Version: 1.0
 */

@Component
public class initCache implements CommandLineRunner {

    private final ApplicationContext applicationContext;


    public initCache(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }


    @Override
    public void run(String... args) throws Exception {
        //获取容器中需要预热的缓存类
        Map<String, AbstractCache> beanMap =
                applicationContext.getBeansOfType(AbstractCache.class);
//        调用init方法
        if (beanMap.isEmpty()) {
            return;
        }
        for (Map.Entry<String, AbstractCache> entry : beanMap.entrySet()) {
            AbstractCache abstractCache = entry.getValue();
            abstractCache.initCache();
        }
    }

}
